package com.cb.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cb.eduservice.entity.EduChapter;
import com.cb.eduservice.entity.EduVideo;
import com.cb.eduservice.entity.chapter.ChapterVo;
import com.cb.eduservice.entity.chapter.VideoVo;
import com.cb.eduservice.mapper.EduChapterMapper;
import com.cb.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Cebb
 * @since 2020-06-22
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    private final EduVideoService videoService;

    @Autowired
    public EduChapterServiceImpl(EduVideoService videoService) {
        this.videoService = videoService;
    }

    @Override
    public List<ChapterVo> getChapterAndVideoByCourseId(String courseId) {
        // 根据课程id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        wrapperChapter.orderByAsc("sort");
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        // 根据课程id查询所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        // 封装
        ArrayList<ChapterVo> result = new ArrayList<>(10);
        for (int i = 0; i < eduChapterList.size(); i++) {
            // 封装章节信息
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);

            // 获取章节id , 用于封装对应的小节信息
            String parentId = eduChapter.getId();
            ArrayList<VideoVo> videoList = new ArrayList<>(10);
            for (int j = 0; j < eduVideoList.size(); j++) {
                EduVideo eduVideo = eduVideoList.get(j);
                if (eduVideo.getChapterId().equals(parentId)) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoList);
            result.add(chapterVo);
        }

        return result;
    }

    @Override
    public boolean deleteChapterById(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        queryWrapper.last("limit 1");
        int count = videoService.count(queryWrapper);
        int flag = 0;
        if (count == 0) {
            flag = baseMapper.deleteById(chapterId);
        }

        return flag > 0;
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
